package com.steel.yfmall.cloud.assistant.goods.impl;

import com.alibaba.fastjson.JSON;
import com.steel.yfmall.cloud.assistant.bean.AssistantConstants;
import com.steel.yfmall.cloud.assistant.common.CacheRedisService;
import com.steel.yfmall.cloud.assistant.dto.para.goods.ParaProductLibCategoryBean;
import com.steel.yfmall.cloud.assistant.goods.ProductLibCategoryService;
import com.steel.yfmall.cloud.assistant.goods.mapper.ProductLibCategoryMapper;
import com.steel.yfmall.cloud.assistant.po.goods.ProductLibCategory;
import com.steel.yfmall.cloud.assistant.po.goods.ProductLibCategoryExample;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yuchaohua
 * @version V1.0
 * @Title: ProductLibCategoryServiceImpl
 * @Package com.steel.yfmall.cloud.assistant.goods.impl
 * @Description: (用一句话描述该文件做什么)
 * @date 2021/1/21 21:22
 */
@Slf4j
@Service
public class ProductLibCategoryServiceImpl implements ProductLibCategoryService {

    /**
     * 获取商品分类基础信息
     * @Title: getProductLibCategory
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param paraBean 参数说明
     * @return com.steel.yfmall.cloud.product.po.ProductLibCategory    返回类型
     * @author yuchaohua
     * @date 2020/11/24 11:50
     */
    @Override
    public ProductLibCategory getProductLibCategory(ParaProductLibCategoryBean paraBean) {
        ProductLibCategoryExample example = new ProductLibCategoryExample();
        getSearchData(paraBean, example);
        ProductLibCategory returnBean = productLibCategoryMapper.selectOneByExampleSelective(example);
        return returnBean;
    }

    /**
     * 获取商品分类信息集合
     * @Title: getProductLibCategoryList
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param paraBean 参数说明
     * @return java.util.List<com.steel.yfmall.cloud.product.po.ProductLibCategory>    返回类型
     * @author yuchaohua
     * @date 2020/11/24 11:47
     */
    public List<ProductLibCategory> getProductLibCategoryList(ParaProductLibCategoryBean paraBean) {
        List<ProductLibCategory> returnListBean = null;
        String redisKey = getProductLibCategoryListCacheKey(paraBean);
        try {
            String value = cacheRedisService.getCommonStringData(redisKey);
            if (StringUtils.isNotBlank(value)) {
                // redis中值不为空则转换为对象
                returnListBean = JSON.parseArray(value, ProductLibCategory.class);
                if (CollectionUtils.isNotEmpty(returnListBean)){
                    return returnListBean;
                }
            }
        } catch (Exception e) {
            log.error("function getProductLibCategoryList", e);
        }
        ProductLibCategoryExample example = new ProductLibCategoryExample();
        getSearchData(paraBean, example);
        if (StringUtils.isNotBlank(paraBean.getOrderByValue())) {
            example.setOrderByClause(paraBean.getOrderByValue());
        }
        returnListBean = productLibCategoryMapper.selectByExample(example);
        // 将数据放入缓存中
        if (CollectionUtils.isNotEmpty(returnListBean)){
            // 在redis中获取值
            cacheRedisService.setRedisData(redisKey, AssistantConstants.REDIS_SAVE_TIME, JSON.toJSONString(returnListBean));
        }
        return returnListBean;
    }

    /**
     * key
     * @param paraBean
     * @return
     */
    public String getProductLibCategoryListCacheKey(ParaProductLibCategoryBean paraBean){
        Map<String, Serializable> params = new HashMap<>();
        params.put("siteCode", AssistantConstants.DEFALUT_SITECODE);
        params.put("paraProductLibCategory", paraBean);
        // 在redis中获取值
        return cacheRedisService.generatorIndex("getProductLibCategoryList", params);
    }

    /**
     * 获取商品分类信息Map
     * @Title: getProductLibCategoryBeanMap
     * @Description: (这里用一句话描述这个方法的作用)
     * @Param paraBean 参数说明
     * @return java.util.Map<java.lang.Integer,com.steel.yfmall.cloud.product.po.ProductLibCategory>    返回类型
     * @author yuchaohua
     * @date 2020/11/24 11:47
     */
    @Override
    public Map<Integer, ProductLibCategory> getProductLibCategoryBeanMap(ParaProductLibCategoryBean paraBean) {
        Map<Integer, ProductLibCategory> returnMapBean = null;
        List<ProductLibCategory> resultListBean = getProductLibCategoryList(paraBean);
        if (CollectionUtils.isNotEmpty(resultListBean)) {
            returnMapBean = new HashMap<>();
            for (ProductLibCategory itemBean : resultListBean) {
                returnMapBean.put(itemBean.getCategoryId(), itemBean);
            }
        }
        return returnMapBean;
    }


    /**
     * 入参拼接
     *
     * @param paraBean
     * @param example
     */
    public void getSearchData(ParaProductLibCategoryBean paraBean, ProductLibCategoryExample example) {
        if (null == example || null == paraBean) {
            return;
        }
        ProductLibCategoryExample.Criteria criteria = example.createCriteria();
        // 站点编码
        if (StringUtils.isNotBlank(paraBean.getSiteCode())) {
            criteria.andSiteCodeEqualTo(paraBean.getSiteCode());
        }
        // 分类名称
        if (StringUtils.isNotBlank(paraBean.getCateName())) {
            criteria.andCateNameEqualTo(paraBean.getCateName());
        }
        // 分类编码
        if (StringUtils.isNotBlank(paraBean.getCateCode())) {
            criteria.andCateCodeEqualTo(paraBean.getCateCode());
        }
        if (CollectionUtils.isNotEmpty(paraBean.getCateCodeList())){
            criteria.andCateCodeIn(paraBean.getCateCodeList());
        }
        // 分类ID
        if (paraBean.getCategoryId() != null) {
            criteria.andCategoryIdEqualTo(paraBean.getCategoryId());
        }
        if (CollectionUtils.isNotEmpty(paraBean.getCategoryIdList())){
            criteria.andCategoryIdIn(paraBean.getCategoryIdList());
        }
        // 父级Id
        if (paraBean.getParentId() != null) {
            criteria.andParentIdEqualTo(paraBean.getParentId());
        }
        if (CollectionUtils.isNotEmpty(paraBean.getParentIdList())){
            criteria.andParentIdIn(paraBean.getParentIdList());
        }
        // 等级
        if (paraBean.getLevel() != null){
            criteria.andLevelEqualTo(paraBean.getLevel());
        }
        // 面包屑
        if (StringUtils.isNotBlank(paraBean.getCrumb())){
            criteria.andCrumbLike(paraBean.getCrumb() +"%");
        }
        // 分类名称
        if (StringUtils.isNotBlank(paraBean.getCateNameLike())){
            criteria.andCateNameLike(paraBean.getCateNameLike() +"%");
        }
    }

    @Resource
    private CacheRedisService cacheRedisService;

    @Resource
    private ProductLibCategoryMapper productLibCategoryMapper;
}
